package by.grsu.as.page;


import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.as.dao.DaoAdmin;
import by.grsu.as.model.Admin;

/**
 * @author Dmitry_Zhivushko
 *
 */
@SuppressWarnings("serial")
public class ListAdminsPage extends BasePage {

	@SpringBean
	private DaoAdmin adminDao;

	public ListAdminsPage() {
		super();

		final List<Admin> admins = adminDao.findAll();

		add(new ListView<Admin>("list", admins) {
			
			@Override
			protected void populateItem(final ListItem<Admin> item) {
				final Admin admin = item.getModelObject();
				// add "labels" to display each property of the item
				item.add(new Label("name", admin.getName()));
				item.add(new Label("lastname", admin.getLastname()));

				// add link which will forward to the "edit" page
				item.add(new Link<Admin>("editlink", new Model<Admin>(admin)) {
					@Override
					public void onClick() {
						setResponsePage(new EditAdminPage(admin));
					}
				});

				// add "link" which removes item from database and reload the
				// page
				item.add(new Link<Admin>("deletelink", new Model<Admin>(admin)) {
					@Override
					public void onClick() {
						adminDao.delete(getModelObject().getId());
						setResponsePage(ListAdminsPage.class);
					}
				});
			}
		});

		// redirect to the "edit" page using "empty" model to create new item as
		// result
		add(new Link<Admin>("createLink", new Model<Admin>(new Admin())) {
			@Override
			public void onClick() {
				setResponsePage(new EditAdminPage(getModelObject()));
			}
		});

	}
}